package net.aaa.hotel.controller;

import net.aaa.hotel.dao.GustRoomDao;
import net.aaa.hotel.dao.ITorderDao;
import net.aaa.hotel.dao.TTuifangDao;
import net.aaa.hotel.entity.TTuifang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/tfang")
public class TTuifangDaoController {
    @Resource
    TTuifangDao tTuifangDao;
    @Resource
    GustRoomDao gustRoomDao;
    @Resource
    ITorderDao torderDao;

    @RequestMapping("append")
    @ResponseBody
    public int appendTuifang(TTuifang tTuifang, HttpServletRequest req){
        int status = 0;
        int i = 0;
        System.out.println("退房 "+tTuifang);
        if(tTuifang.getRuzhuid()==null){
            String c = req.getParameter("count");
            // 总钱数/房间数  每个房间该该退的钱数
//            i += tTuifangDao.addTuifang(tTuifang);

            // 先要获取退房记录中对应订单id的数量，判断是否大于对应房间的数量
            int count = Integer.parseInt(c);
            tTuifang.setTsmoney(tTuifang.getTsmoney()/count);

            for(int j = 0;j<count;j++){
                System.out.println("取消订单的记录==== "+tTuifang);
                i += tTuifangDao.addTuifang(tTuifang);
            }
            status += torderDao.changeOrderStatus(10, tTuifang.getOrderid());

        }else {
            i += tTuifangDao.addTuifang(tTuifang);
            Map map = tTuifangDao.getTFCountByOrderId(tTuifang.getOrderid());
            System.out.println(map);
            if(map.get("count")!=null){
                int a = Integer.parseInt(map.get("a") + "");
                int anInt = Integer.parseInt(map.get("count") + "");

                if(anInt==a){
                    // 改变订单的状态
                    status = torderDao.changeOrderStatus(2, tTuifang.getOrderid());
                }
            }
        }

        return i+status;
    }

    @RequestMapping("changeroom")
    @ResponseBody
    public int changeRoom(Integer id){
        System.out.println(id);
        int status = gustRoomDao.changeRoomStatusById(1, id);
        return status;
    }
}
